import enUS from 'components/locale/en_US'
import zhCN from 'components/locale/zh_CN'

/* eslint no-useless-escape:0 */
const reg = /(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/

/**
 * 判断字符串是否为合法的url地址
 * @param path 路径
 * @returns {boolean} true: 合法url路径 false： 不合法url路径
 */
export function isUrl(path) {
  return reg.test(path)
}

const messages = {
  enUS,
  zhCN,
}

/**
 * 根据本地语言获取国际化信息
 * @param code 信息编码
 * @returns {*} 本地信息内容或者code本身
 */
export function getMessage(code) {
  const language = localStorage.getItem('language') === 'zh_CN' ? 'zhCN' : 'enUS'
  return messages[language][code] ? messages[language][code] : code
}
